/**
 * 
 */
package com.yidu.user.view;



import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.model.ManagePayIn;
import com.yidu.model.ManageUser;
import com.yidu.model.Page;
import com.yidu.model.Plot;
import com.yidu.user.service.ManagePayOutService;
import com.yidu.user.service.ManageUserService;
import com.yidu.user.service.MangePayInService;

@Controller
@RequestMapping("/payIn")
public class ManagePayInController {
	@Resource(name="payInService")
	private MangePayInService managePayInService;
	@Resource(name="payOutService")
	private ManagePayOutService payOutService;
	@Resource(name="userService")
	private ManageUserService userService;
	
	@RequestMapping("/list")
	public String showList(HttpServletRequest request,Page page,ManagePayIn pay){
		
		List<ManagePayIn> list = managePayInService.getAllPayIn(page,pay);
		request.setAttribute("list", list);
		request.setAttribute("date", page.getName());
		return "payInList";
	}
	@RequestMapping("/delete")
	public void delete(String payinId,String usName,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		int row = managePayInService.deleteByPayInId(payinId,usName);
		PrintWriter out = response.getWriter();
		if(row>0){
			out.print("ok");
		}
		out.close();
	}
	@ResponseBody
	@RequestMapping("/showUpdate")
	public ManagePayIn showUpdate(ManagePayIn payIn) {
		String id = payIn.getPayinId();
		ManagePayIn pay = managePayInService.showUpdate(id);
		return pay;
	}
	@RequestMapping("/addOrUpdate")
	public String addOrUpdate(ManagePayIn payIn,HttpServletRequest request,Page page){
		page.setUserId(payIn.getUsName());
		managePayInService.updateByPayInId(payIn);
		List<ManagePayIn> list = managePayInService.getAllPayIn(page,payIn);
		request.setAttribute("list", list);
		request.setAttribute("date", page.getName());
		return "payInList";
	}
	@RequestMapping("/statistice")
	public String statistice(HttpServletRequest request){
		//用户名
		//查询出所有的用户名
		List<ManageUser> list1 = userService.getAllName();
		String line1 = "[";
		for (int i = 0; i < list1.size(); i++) {
			ManageUser us = list1.get(i);
			line1 += "'"+us.getUserName()+"',";
		}
		line1 = line1.substring(0,line1.length()-1);
		line1 += "]";
		request.setAttribute("line1", line1);
		String line="[";
		for (int i = 0; i < list1.size(); i++) {
			ManageUser us = list1.get(i);
			List<Plot> list = managePayInService.selectMoney(us.getUserId());
			//收入
			
			for (int j = 0; j < list.size(); j++) {
				Plot p = list.get(j);
				if(p==null){
					Plot pl = new Plot();
					pl.setValue("0");
					line += pl.getValue()+",";
				}else{
					line += p.getValue()+",";
				}
			}
		}
		line = line.substring(0,line.length()-1);
		line += "]";
		request.setAttribute("line", line);
		
		String line2 = "[";
		for (int i = 0; i < list1.size(); i++) {
			ManageUser us = list1.get(i);
			//支出
			List<Plot> list2 = payOutService.selectMoney(us.getUserId());
			
			for (int j = 0; j < list2.size(); j++) {
				Plot p = list2.get(j);
				if(p==null){
					Plot pl = new Plot();
					pl.setValue("0");
					line2 += pl.getValue()+",";
				}else{
					line2 += p.getValue()+",";
				}
			}
		}
		
		line2 = line2.substring(0, line2.length()-1);
		line2 += "]";
		request.setAttribute("line2", line2);
		return "statistics";
	}
}
